﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Windows.Markup;
using Microsoft.Win32;
using Microsoft.Surface.Presentation;
using Microsoft.Surface.Presentation.Controls;
using Microsoft.Surface.Presentation.Input;
using System.Xml;
using System.Globalization;
using System.IO;
using System.Timers;
using System.ComponentModel;
using Ab2d.Common.ReaderWmf;
namespace MultiTouchDemonstrator
{
    /// <summary>
    /// Interaction logic for MapArea.xaml
    /// </summary>
    public partial class MapArea : UserControl
    {
        private LibraryContainer m_libraryContainer;
        private Image target = null;
        private Matrix rectsMatrix ;
        private Point orgCenter;
        public MapArea()
        {
            InitializeComponent();
            

            
        }
        //public ScrollViewer GetScroll { get { return mapScrollbar; } set { mapScrollbar = value; } }
        public LibraryContainer SymbolContainer { 
            get{return m_libraryContainer;}
            set{m_libraryContainer = value;} 
        }
        public Canvas MapCanvas
        {
            get { return m_map; }
            set { m_map = value; }
        }

        private void m_map_Drop(object sender, DragEventArgs e)
        {
            ItemsControl itemsControl = sender as ItemsControl;
            Image img = new Image();
            img.Source = new BitmapImage(new Uri((string)e.Data.GetData(typeof(string)), UriKind.Absolute));
            m_map.Children.Add(img);
            TranslateTransform translation = new TranslateTransform(e.GetPosition(m_map).X, e.GetPosition(m_map).Y);
            RotateTransform rotation = new RotateTransform(0);
            TransformGroup group = new TransformGroup();
            group.Children.Add(translation);
            group.Children.Add(rotation );
            img.RenderTransform = group;
            foreach (var node in m_libraryContainer.ItemsSource)
            {
                m_libraryContainer.SetIsItemDataEnabled(node, true);

            }

        }

        private void m_map_Drop(object sender, SurfaceDragDropEventArgs e)
        {
            ItemsControl itemsControl = sender as ItemsControl;
            Symbol img = new Symbol();
            img.Tag = 0;
            img.Source = new BitmapImage(new Uri(((string)e.Cursor.Data), UriKind.Absolute));
            m_map.Children.Add(img);
            img.Width = 50;
            
            target= null;
            foreach (Image item in m_map.Children) {
                if (((int)item.Tag) == 1)
                    target = item;
            }
            if (target != null)
            {
                rectsMatrix = ((MatrixTransform)target.RenderTransform).Matrix;
                Timer time = new Timer(500);
                //double xtarget = e.Cursor.GetPosition(m_map).X-e.Cursor.GetPosition(target).X;
                //double ytarget = e.Cursor.GetPosition(m_map).Y-e.Cursor.GetPosition(target).Y;
                orgCenter = new Point(e.Cursor.GetPosition(m_map).X, e.Cursor.GetPosition(m_map).Y);//new Point(rectToMove.ActualWidth / 2, rectToMove.ActualHeight / 2);
                // Find the new center.
                Point center = rectsMatrix.Transform(orgCenter);
                rectsMatrix.ScaleAt(2, 2, orgCenter.X, orgCenter.Y);
                //Point symboleCentre = new Point(img.ActualWidth/2,img.ActualHeight/2);
                // Apply the changes to the Rectangle.
                target.RenderTransform = new MatrixTransform(rectsMatrix);
                TranslateTransform translation = new TranslateTransform(e.Cursor.GetPosition(m_map).X, e.Cursor.GetPosition(m_map).Y);
                img.Center = new Point(e.Cursor.GetPosition(target).X, e.Cursor.GetPosition(target).Y);
                RotateTransform rotation = new RotateTransform(0);
                TransformGroup group = new TransformGroup();
                group.Children.Add(translation);
                group.Children.Add(rotation);
                img.RenderTransform = group;
                foreach (var node in m_libraryContainer.ItemsSource)
                {
                    m_libraryContainer.SetIsItemDataEnabled(node, true);

                }
                time.Elapsed += new ElapsedEventHandler(time_Elapsed);
                time.Enabled = true;
                time.Start();
                
 
            }
        }

        void time_Elapsed(object sender, ElapsedEventArgs e)
        {
            rectsMatrix.ScaleAt(0.5, 0.5, orgCenter.X, orgCenter.Y);
            //target.RenderTransform = new MatrixTransform(rectsMatrix);
            Timer time = sender as Timer;
            time.Close();
        }
       

        //private void DragCanceled(object sender, SurfaceDragDropEventArgs e)
        //{
        //    ScatterViewItem item = e.Cursor.Data as ScatterViewItem;
        //    if (item != null)
        //    {
        //        item.Visibility = Visibility.Visible;
        //        item.Orientation = e.Cursor.GetOrientation(this);
        //        item.Center = e.Cursor.GetPosition(this);
        //    }
        //}
        //private void scatterView1_Loaded(object sender, RoutedEventArgs e)
        //{


        //}
        //private void scatterView1_Drop(object sender, SurfaceDragDropEventArgs e)
        //{
        //    ItemsControl itemsControl = sender as ItemsControl;
        //    Image img = new Image();
        //    img.Source = new BitmapImage(new Uri((string)e.Cursor.Data, UriKind.Absolute));
        //    ScatterViewItem scatterViewItem = new ScatterViewItem();
        //    scatterViewItem.Content = img;
        //    scatterViewItem.Width = 100;
        //    //scatterViewItem.LayoutTransform = new TranslateTransform(e.Cursor.GetPosition(scatterView1).X, e.Cursor.GetPosition(scatterView1).Y);

        //    //Define an image size

        //    //scatterViewItem.Width = e.Cursor.GetPosition(scatterView1).X;
        //    //scatterViewItem.Height = e.Cursor.GetPosition(scatterView1).Y;


        //    scatterViewItem.CanMove = true;

        //    scatterView1.Items.Add(scatterViewItem);
        //    scatterViewItem.Center = new Point(e.Cursor.GetPosition(scatterView1).X, e.Cursor.GetPosition(scatterView1).Y);
        //    scatterViewItem.Orientation = 0;
        //    foreach (var node in m_libraryContainer.ItemsSource)
        //    {
        //        m_libraryContainer.SetIsItemDataEnabled(node, true);

        //    }
        //}
        //private void DragSourcePreviewInputDeviceDown(object sender, InputEventArgs e)
        //{
        //    DragDropEffects effects;

        //    DataObject obj = new DataObject();

        //    FrameworkElement findSource = e.OriginalSource as FrameworkElement;
        //    ScatterViewItem draggedElement = null;
        //    while (draggedElement == null && findSource != null)
        //    {
        //        if ((draggedElement = findSource as ScatterViewItem) == null)
        //        {
        //            findSource = VisualTreeHelper.GetParent(findSource) as FrameworkElement;
        //        }
        //    }

        //    if (draggedElement == null)
        //    {
        //        return;
        //    }
        //    //if (findSource != null)
        //    //{
        //    obj.SetData(typeof(ScatterViewItem), findSource as ScatterViewItem);



        //    effects = DragDrop.DoDragDrop(this, obj, DragDropEffects.Move);

        //    // }
        //}
    }
}
